﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace CSharpHelpers.IOs
{
    class FileHelper
    {
        /// <summary>
        /// 删除本地文件
        /// </summary>
        /// <param name="imagesName"></param>
        public static void DeleteLocalFile(string imagesName)
        {
            try
            {
                string imageUrl = TmpImagesUrl(imagesName);
                if (File.Exists(imageUrl))
                {
                    File.Delete(imageUrl);
                }
            }
            catch (Exception e)
            {
                SystemLog.Error("刪除本地臨時文件異常: " + e);
            }
        }

        /// <summary>
        /// 删除本地文件
        /// </summary>
        /// <param name="tmpList"></param>
        public static void DeleteLocalFile(List<string> tmpList)
        {
            try
            {
                foreach (string t in tmpList)
                {
                    if (File.Exists(TmpImagesUrl(t)))
                    {
                        File.Delete(TmpImagesUrl(t));
                    }
                }
            }
            catch (Exception e)
            {
                SystemLog.Error("刪除本地臨時文件異常: " + e);
            }
        }

        /// <summary>
        /// 临时照片路径
        /// </summary>
        /// <param name="imagesName"></param>
        /// <returns></returns>
        public static string TmpImagesUrl(string imagesName)
        {
            string imageUrl = Application.StartupPath + String.Format("\\{0}", imagesName);
            return imageUrl;
        }

        public static byte[] GetFielBytes(string path)
        {
            byte[] arrFile = null;
            if (File.Exists(path))
            {
                using (var fs = new FileStream(path, FileMode.Open))
                {
                    arrFile = new byte[fs.Length];
                    fs.Read(arrFile, 0, arrFile.Length);
                }
            }

            return arrFile;
        }

        public static byte[] ReadFile(string path)
        {
            int fileSize;
            byte[] rawData; FileStream fs;
            try
            {
                fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                fileSize = (int)fs.Length;
                rawData = new byte[fileSize];
                fs.Read(rawData, 0, fileSize);
                fs.Close();
                fs.Dispose();
            }
            catch (Exception)
            {
                return null;
            } fs = null; return rawData;
        }

        public static string ReadData()
        {
            string money = string.Empty;
            if (File.Exists("SetMoney.txt"))
            {
                FileStream fs = new FileStream("SetMoney.txt", FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                string str = sr.ReadLine();
                money = str;
                sr.Close();
                fs.Close();
            }

            return money;
        }

        public static void WriteData(string value)
        {
            if (File.Exists("SetMoney.txt"))
            {
                StreamReader sr = new StreamReader("SetMoney.txt", Encoding.UTF8);
                string str = sr.ReadToEnd();
                sr.Close();
                File.WriteAllText("SetMoney.txt", value, Encoding.UTF8);
            }
            else
            {

            }
        }

        public static void CreateData(string value)
        {
            if (!File.Exists("SetMoney.txt"))
            {
                FileStream createFile = new FileStream("SetMoney.txt", FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(createFile);
                sw.WriteLine(value);
                sw.Close();
                createFile.Close();
            }
        }


        /// <summary>
        /// 把 temp 合并到 total 里面
        /// </summary>
        /// <param name="total"></param>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static byte[] CopyBuffer(byte[] total, byte[] temp)
        {
            if (temp == null)
                return new byte[0];
            byte[] newTotal = new byte[(long)total.Length + (long)temp.Length];
            total.CopyTo(newTotal, 0);
            temp.CopyTo(newTotal, total.Length);
            return newTotal;
        }

        public static List<KeyValuePair<string, string>> GetFiles(string path)
        {
            var list = new List<KeyValuePair<string, string>>();
            GetFiles(path, ref list);
            return list;
        }

        /// <summary>
        /// 递归获取所有文件名包含子收件夹里面的文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileList"></param>
        private static void GetFiles(string path, ref List<KeyValuePair<string, string>> fileList)
        {
            if (Directory.Exists(path))
            {
                DirectoryInfo folder = new DirectoryInfo(path);
                foreach (FileInfo file in folder.GetFiles())
                {
                    fileList.Add(new KeyValuePair<string, string>(file.Name, file.FullName));
                }
                string[] strDirectories = Directory.GetDirectories(path);

                foreach (string dir in strDirectories)
                {
                    GetFiles(dir, ref fileList);
                }
            }
        }

    }
}
